package com.liang.maogouyuyin;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModelProvider;

import com.liang.maogouyuyin.local.LocalRepository;
import com.liang.maogouyuyin.ui.viewmodel.AnomalyViewModel;
import com.liang.maogouyuyin.ui.viewmodel.CasebookViewModel;
import com.liang.maogouyuyin.ui.viewmodel.ExpenditureViewModel;
import com.liang.maogouyuyin.ui.viewmodel.PetMenuViewModel;
import com.liang.maogouyuyin.ui.viewmodel.PetViewModel;
import com.liang.maogouyuyin.ui.viewmodel.WeightViewModel;
import com.liang.publics.base.BaseApplication;
import com.liang.publics.config.Const;
import com.liang.publics.util.LogUtil;
import com.liang.publics.util.MyStringUtil;
import com.liang.publics.util.SharedPreferencesUtil;
import com.liang.publics.util.UmengUtil;
import com.liang.publics.util.oaid.DemoHelper;
import com.liang.publics.util.oaid.DemoHelperHuawei;
import com.liang.publics.util.oaid.StringId;
import com.liang.maogouyuyin.utils.dj.CountdownTimeTask;

import java.util.TimerTask;


/**
 * Custom application.
 * @Author Blueskylct
 * @version 1.1
 */
public class TheBaseApplication extends BaseApplication implements Application.ActivityLifecycleCallbacks, DemoHelper.AppIdsUpdater, DemoHelperHuawei.AppIdsUpdater, UmengUtil.AppIdsUpdater {

    @SuppressLint("StaticFieldLeak")
    public static Context context;
    @SuppressLint("StaticFieldLeak")
    private static TheBaseApplication instance;
    public static int runningActivities = 0;
    public   static  boolean ifMainActivity = false;
    private boolean isAppInBackground = true;

    private PetViewModel petViewModel;
    private ExpenditureViewModel expenditureViewModel;
    private WeightViewModel weightViewModel;
    private AnomalyViewModel anomalyViewModel;
    private CasebookViewModel casebookViewModel;
    private PetMenuViewModel petMenuViewModel;

    public static TheBaseApplication getInstance() {
        if (instance == null) {
            instance = new TheBaseApplication();
        }
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        instance = this;
        context = getApplicationContext();
        LocalRepository.INSTANCE.init(context);

        petViewModel = ViewModelProvider.AndroidViewModelFactory.getInstance(this).create(PetViewModel.class);
        expenditureViewModel = ViewModelProvider.AndroidViewModelFactory.getInstance(this).create(ExpenditureViewModel.class);
        weightViewModel = ViewModelProvider.AndroidViewModelFactory.getInstance(this).create(WeightViewModel.class);
        anomalyViewModel = ViewModelProvider.AndroidViewModelFactory.getInstance(this).create(AnomalyViewModel.class);
        casebookViewModel = ViewModelProvider.AndroidViewModelFactory.getInstance(this).create(CasebookViewModel.class);
        petMenuViewModel = ViewModelProvider.AndroidViewModelFactory.getInstance(this).create(PetMenuViewModel.class);

        if (SharedPreferencesUtil.getAgreementStatus()) {
            String oaid = SharedPreferencesUtil.getString("oaid");
            //缓存唯一标识
            StringId.idCache(this);
            if (oaid ==null || oaid.isEmpty()) {
                SplashActivity.mymanufacturer = MyStringUtil.getmanufacturer(Build.MANUFACTURER);

                try {
                    new UmengUtil().getOaid(this,this);
                } catch (Exception e){
                    e.printStackTrace();
                }

                try {
                    if("huawei".equals(SplashActivity.mymanufacturer)) {
                        new DemoHelperHuawei().getOaid(this, this);
                    }
                } catch (Exception e){
                    e.printStackTrace();
                }

                try {
                    //初始化获取oaid
                    SplashActivity.demoHelper = new DemoHelper(this, Const.lib);
                    SplashActivity.demoHelper.getDeviceIds(this, true, false, false);
                } catch (Exception e){
                    e.printStackTrace();
                }
            }
        }

        registerActivityLifecycleCallbacks(this);
        setTimeCountdown();
    }

    public static Context getTheBaseApplicationContext() {
        return context;
    }

    public static boolean isAppInForeground() {
        return runningActivities > 0;
    }

    @Override
    public void onActivityPaused(@NonNull Activity activity) {
        Log.i(Const.TAG,"DaysApplication-------onActivityPaused");
        // 应用程序切换到后台
        isAppInBackground = true;
        // 执行相应的操作
    }

    @Override
    public void onActivityResumed(@NonNull Activity activity) {
        Log.i(Const.TAG,"DaysApplication-------onActivityResumed");
        // 应用程序回到前台
        if (isAppInBackground) {
            // 执行相应的操作
            //SplashActivity.getConfigAd();
        }
        isAppInBackground = false;
    }

    @Override
    public void onActivityCreated(@NonNull Activity activity, Bundle savedInstanceState) {
        Log.i(Const.TAG,"DaysApplication-------onActivityCreated");
    }

    @Override
    public void onActivityStarted(@NonNull Activity activity) {
        Log.i(Const.TAG,"DaysApplication-------onActivityStarted");
        if (runningActivities == 0) {
            Log.i(Const.TAG,"DaysApplication-------onActivityStarted----切换到前台++++ifMainActivity："+ifMainActivity);
            if (ifMainActivity) {
                if (SharedPreferencesUtil.getStatus(SharedPreferencesUtil.IF_AD)) {
                    Intent intent = new Intent(this, SplashActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    this.startActivity(intent);
                }
            }
        }
        runningActivities++;
    }

    @Override
    public void onActivityStopped(@NonNull Activity activity) {
        Log.i(Const.TAG,"DaysApplication-------onActivityStopped");
        runningActivities--;
        if (runningActivities == 0) {
            Log.i(Const.TAG,"DaysApplication-------onActivityStopped---切换到后台");
        }
    }

    @Override
    public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {
        Log.i(Const.TAG,"DaysApplication-------onActivitySaveInstanceState");
    }

    @Override
    public void onActivityDestroyed(@NonNull Activity activity) {
        Log.i(Const.TAG,"DaysApplication-------onActivityDestroyed");
    }

    @Override
    public void onIdsValid(String oaid, String vaid, String aaid) {
        Log.d(Const.TAG, "onIdsValid--11111111111111----oaid:"+oaid);
    }

    @Override
    public void onIdsValid(String oaid) {
        Log.d(Const.TAG, "onIdsValid---huawei--11111111111111----oaid:"+oaid);
    }

    @Override
    public void onIdsValidUm(String oaid) {
        Log.d(Const.TAG, "onIdsValid---ym--11111111111111----oaid:"+oaid);
    }

    private void setTimeCountdown() {
        CountdownTimeTask countdownTimeTask = new CountdownTimeTask(1000 * 15*60, new TimerTask() {
            @Override
            public void run() {
                LogUtil.d(Const.TAG, "CountdownTimeTask调用了----------"+ TheBaseApplication.ifMainActivity +"-----"+SharedPreferencesUtil.getStatus(SharedPreferencesUtil.IF_AD));
                //if (SharedPreferencesUtil.getStatus(SharedPreferencesUtil.IF_AD)) {
                if (TheBaseApplication.ifMainActivity) {
                    SplashActivity.updateConfigAd(SplashActivity.activity);
                }
                //}
            }
        });
        countdownTimeTask.start();
    }

    public PetViewModel getPetViewModel() {
        return petViewModel;
    }

    public ExpenditureViewModel getExpenditureViewModel() {
        return expenditureViewModel;
    }

    public WeightViewModel getWeightViewModel() {
        return weightViewModel;
    }

    public AnomalyViewModel getAnomalyViewModel() {
        return anomalyViewModel;
    }

    public CasebookViewModel getCasebookViewModel() {
        return casebookViewModel;
    }

    public PetMenuViewModel getPetMenuViewModel() {
        return petMenuViewModel;
    }
}